package com.info3k.client.commands.user;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.info3k.client.Info3K;
import com.info3k.client.components.ContentBox;
import com.info3k.client.domain.User;
import com.info3k.client.enums.UserTypes;


public class ShowUserCommand extends ContentBox implements Command {
	
	private Button editButton = new Button(Info3K.msgs.editUser());
	
	private Info3K info3k;
	
	private User user;
	
	public ShowUserCommand(Info3K info, User us){
		this.info3k = info;
		this.user = us;
		content.setWidth("100%");
		
		if(info3k.getLoginManager().getUser()!=null){
			if(info3k.getLoginManager().getUser().getUsername().equals(user.getUsername())){
				setText(Info3K.msgs.settings());
			} else {
				setText(Info3K.msgs.editUser());
				
			}
		}

		if(user.getName()!=null && user.getUsername()!=null)
			setText(user.getName() + " " + user.getSurname());
		else if(user.getUsername()!=null)
			setText(user.getUsername());
		
		final HTML lblUsername = new HTML("<b>" + Info3K.msgs.username() + ":</b> " + user.getUsername());
		final HTML lblEmail = new HTML("<b>" + Info3K.msgs.email() + ":</b> " + user.getEmail());
		final HTML lblName = new HTML("<b>" + Info3K.msgs.uName() + ":</b> " + user.getName());
		final HTML lblSurname = new HTML("<b>" + Info3K.msgs.surname() + ":</b> " + user.getSurname());
		final HTML lblPhone = new HTML("<b>" + Info3K.msgs.phone() + ":</b> " + user.getPhone());
		final HTML lblUserType = new HTML("<b>" + Info3K.msgs.userType() + ":</b> " + user.getUserType().toString());
		
		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setWidth("100%");
		VerticalPanel vPanel1 = new VerticalPanel();
		VerticalPanel vPanel2 = new VerticalPanel();
		vPanel1.setWidth("100%");
		vPanel2.setWidth("100%");
		content.add(hPanel);
		hPanel.add(vPanel1);
		hPanel.add(vPanel2);
		
		if(user.getUsername()!=null && user.getUsername().length()>0) vPanel1.add(lblUsername);
		if(user.getUserType()!=null && user.getUserType().toString().length()>0) vPanel1.add(lblUserType);
		if(user.getEmail()!=null && user.getEmail().length()>0) vPanel1.add(lblEmail);
		if(user.getName()!=null && user.getName().length()>0) vPanel2.add(lblName);
		if(user.getSurname()!=null && user.getSurname().length()>0) vPanel2.add(lblSurname);
		if(user.getPhone()!=null && user.getPhone().length()>0) vPanel2.add(lblPhone);

		if(info3k.getLoginManager().getUser()!=null){
			if(info3k.getLoginManager().getUser().getUserType().equals(UserTypes.ADMINISTRATOR) || user.getUsername().equals(info3k.getLoginManager().getUser().getUsername())){
				headerRight.add(editButton);
				headerRight.setCellHorizontalAlignment(editButton, HorizontalPanel.ALIGN_RIGHT);
			}
		}
		
		editButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				EditUserCommand cmd = new EditUserCommand(info3k, user);
				cmd.execute();
			}
		});
	}

	public void execute() {
		center();
	}
}